# @Time : 2021/5/30 16:43
# @Author : Li Kunlun
# @Description : Synthetic Objective Functions and ZDT1

import numpy as np  # for multi-dimensional containers
import pandas as pd  # for DataFrames
import plotly.graph_objects as go  # for data visualisation

# Optional Customisations
import plotly.io as pio  # to set shahin plot layout

pio.templates['shahin'] = pio.to_templated(go.Figure().update_layout(
    legend=dict(orientation="h", y=1.1, x=.5, xanchor='center'),
    margin=dict(t=0, r=0, b=40, l=40))).layout.template
pio.templates.default = 'shahin'
pio.renderers.default = "notebook_connected"  # remove when running locally

D = 30
x = np.random.rand(D)
# print(x)


def ZDT1(x):
    f1 = x[0]  # objective 1
    g = 1 + 9 * np.sum(x[1:D] / (D - 1))
    h = 1 - np.sqrt(f1 / g)
    f2 = g * h  # objective 2
    return [f1, f2]


objective_values = ZDT1(x)
print(objective_values)

true_front = np.empty((0, 2))

for f1 in np.linspace(0, 1, num=20):
    f2 = 1 - np.sqrt(f1)
    true_front = np.vstack([true_front, [f1, f2]])

true_front = pd.DataFrame(true_front, columns=['f1', 'f2'])  # convert to DataFrame
print(true_front)

fig = go.Figure(layout=dict(xaxis=dict(title='f1'), yaxis=dict(title='f2')))
fig.add_scatter(x=true_front.f1, y=true_front.f2, mode='markers')
fig.update_traces(diagonal_visible=False)
fig.show()
